Ext.onReady(function(){ 

Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'under';


function getBodyWidth(){  
    return document.body.clientWidth-15;  
}  
  

var _bodyWidth = getBodyWidth();  
var _columns = 5;  
var _perWidth = _bodyWidth/_columns;  
    
var addForm=new Ext.FormPanel({   
        id:'addForm',  
        frame:true,  
        layout:'table',   
        style:'height:100%,width:100%',  
        layoutConfig: {columns:_columns},  
        //title:'TableForm',
        defaults:{width:_perWidth}, 
        items:[
	        {
			    items:[{
				    	layout : "form",
					    labelWidth: 80,
					    items : [{
							xtype: 'textfield',
							id: 'describe1', 
							name: 'describe1', 
							fieldLabel: '特征项信息',
							allowBlank: false,
							blankText:'请输入特征项信息',    
							anchor:'96%' 
						}]
				}]
		    },
//		    {
//			    items:[{
//				    	layout : "form",
//					    labelWidth: 60,
//					    items : [{
//							xtype: 'textfield',
//							id: 'describe2', 
//							name: 'describe2', 
//							fieldLabel: '子类因素',
//							allowBlank: false,
//							blankText:'请输入子类因素',     
//							anchor:'96%' 
//						}]
//				}]
//		    },
		    {
			    items:[{
				    	layout : "form",
					    labelWidth: 80,
					    items : [{
							xtype: 'textfield',
							id: 'describe3', 
							name: 'describe3', 
							fieldLabel: '特征项说明',
							allowBlank: true,   
							anchor:'96%' 
						}]
				}]
		    },
		     {
			    items:[{
				    	layout : "form",
					    labelWidth: 20,
					    items : [{
							xtype: 'textfield',
							id: 'itemDescribe1', 
							name: 'itemDescribe1', 
							fieldLabel: 'A',
							allowBlank: true,    
							anchor:'96%' 
							},
							{
							xtype: 'textfield',
							id: 'itemDescribe2', 
							name: 'itemDescribe2', 
							fieldLabel: 'B',
							allowBlank: true,   
							anchor:'96%' 
							},
							{
							xtype: 'textfield',
							id: 'itemDescribe3', 
							name: 'itemDescribe3', 
							fieldLabel: 'C',
							allowBlank: true,    
							anchor:'96%' 
							},
							{
							xtype: 'textfield',
							id: 'itemDescribe4', 
							name: 'itemDescribe4', 
							fieldLabel: 'D',
							allowBlank: true,    
							anchor:'96%' 
							}
							,
							{
							xtype: 'textfield',
							id: 'itemDescribe5', 
							name: 'itemDescribe5', 
							fieldLabel: 'E',
							allowBlank: true,    
							anchor:'96%' 
							}
							,
							{
							xtype: 'textfield',
							id: 'itemDescribe6', 
							name: 'itemDescribe6', 
							fieldLabel: 'F',
							allowBlank: true,    
							anchor:'96%' 
							}
							,
							{
							xtype: 'textfield',
							id: 'itemDescribe7', 
							name: 'itemDescribe7', 
							fieldLabel: 'G',
							allowBlank: true,    
							anchor:'96%' 
							}
						]
				}]
		    },{
			    items:[{
				    	layout : "form",
					    labelWidth: 40,
					    items : [{
							xtype: 'textfield',
							id: 'itemValue1', 
							name: 'itemValue1', 
							fieldLabel: '得分',
							allowBlank: true,    
							anchor:'96%' 
							},
							{
							xtype: 'textfield',
							id: 'itemValue2', 
							name: 'itemValue2', 
							fieldLabel: '得分',
							allowBlank: true,   
							anchor:'96%' 
							},
							{
							xtype: 'textfield',
							id: 'itemValue3', 
							name: 'itemValue3', 
							fieldLabel: '得分',
							allowBlank: true,    
							anchor:'96%' 
							},
							{
							xtype: 'textfield',
							id: 'itemValue4', 
							name: 'itemValue4', 
							fieldLabel: '得分',
							allowBlank: true,    
							anchor:'96%' 
							},
							{
								xtype: 'textfield',
								id: 'itemValue5', 
								name: 'itemValue5', 
								fieldLabel: '得分',
								allowBlank: true,    
								anchor:'96%' 
							},
							{
								xtype: 'textfield',
								id: 'itemValue6', 
								name: 'itemValue6', 
								fieldLabel: '得分',
								allowBlank: true,    
								anchor:'96%' 
							},
							{
								xtype: 'textfield',
								id: 'itemValue7', 
								name: 'itemValue7', 
								fieldLabel: '得分',
								allowBlank: true,    
								anchor:'96%' 
							}
							
						]
				}]
		    }
		 ],
	buttons:[{text:'添加',handler:func_submit_onclick},
	{text:'完成'}
	]   
    });   
    
var config={
    title:"提示",
    width:240,
    msg:'<div align="center">操作成功！</div>',
    closable:false,
    buttons:{yes:'确定'},
    fn: function(btn){
    	//alert('单击的按钮id是：'+btn);
    	if(btn == 'no'){
    		var nowDescribe1 = Ext.getCmp('describe1').getValue();
    		addForm.form.reset();
    		Ext.getCmp('describe1').setValue(nowDescribe1);
    		grid.store.reload(); 
    	}
    	else if(btn == 'yes'){
    		addForm.form.reset();
    		grid.store.reload(); 
    	}
    	
    }
    //icon:Ext.MessageBox.Question
}



function func_submit_onclick(){  
    if(addForm.form.isValid()){
		addForm.form.doAction('submit', { 
			url : 'model/addAdjustment.do',
			method : 'POST',
			success : function() {
				var msgbox = Ext.MessageBox.show(config); 
				msgbox.getDialog().moveTo(100, 100); 
				//setPosition(event.x - 300 ,event.y + 100); 
				//Ext.Msg.alert("提示", "操作成功！");
				grid.store.reload(); 
			},
			failure : function() {
				Ext.MessageBox.alert("请求异常！");
			}
		});
	}    
}  

var fm = Ext.form;

//Gridpanel
var cm=new Ext.grid.ColumnModel([
{header:'流水号',dataIndex:'serialNo',hidden:true,
editor: new fm.TextField({allowBlank: false,editable:false,readOnly:true})
} ,
{header:'特征项信息',
	sortable: true, renderer: function (value, meta, record, rowIndex, colIndex, store) {
                    var first = !rowIndex || value !== store.getAt(rowIndex - 1).get('describe1'),
                        last = rowIndex >= store.getCount() - 1 || value !== store.getAt(rowIndex + 1).get('describe1');
                    meta.css += 'row-span' + (first ? ' row-span-first' : '') +  (last ? ' row-span-last' : '');
                    if (first) {
                        var i = rowIndex + 1;
                        while (i < store.getCount() && value === store.getAt(i).get('describe1')) {
                            i++;
                        }
                        var rowHeight = 20, padding = 6,
                            height = (rowHeight * (i - rowIndex) - padding) + 'px';
                        meta.attr = 'style="height:' + height + ';line-height:' + height + ';"';
                    }
                    return first ? value : '';
                }, 
	dataIndex:'describe1',
	width:80,
	editor: new fm.TextField({
                allowBlank: false
            })
    },
{header:'特征项说明',
	sortable: true, renderer: function (value, meta, record, rowIndex, colIndex, store) {
                    var first = !rowIndex || value !== store.getAt(rowIndex - 1).get('describe3'),
                        last = rowIndex >= store.getCount() - 1 || value !== store.getAt(rowIndex + 1).get('describe3');
                    meta.css += 'row-span' + (first ? ' row-span-first' : '') +  (last ? ' row-span-last' : '');
                    if (first) {
                        var i = rowIndex + 1;
                        while (i < store.getCount() && value === store.getAt(i).get('describe3')) {
                            i++;
                        }
                        //alert("i: "+i+" rowIndex:"+rowIndex);
                        var rowHeight = 20, padding = 6,
                            height = (rowHeight * (i - rowIndex) - padding) + 'px';
                        meta.attr = 'style="height:' + height + ';white-space:normal;"';
                    }
                    return first ? value : '';
                },
dataIndex:'describe3',
width:110,
editor: new fm.TextField({
                allowBlank: false
            })
}, 
{header:'描述',
dataIndex:'itemDescribe',
width:90,
editor: new fm.TextField({
                allowBlank: false
}),
renderer: function (value, metadata, record){
	metadata.attr = 'style="white-space:normal;"'; 
	return value; 
},
},
{header:'选项得分',
dataIndex:'itemValue',
width:40,
editor: new fm.NumberField({
                allowBlank: false,
                allowNegative: false
            })
}

]); 

var ds = new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'model/listModelItems.do?dataFlag=3',
method:'POST'
}), 
reader:new Ext.data.JsonReader({ 
totalProperty:'total',  
root:'resultData'},[
{name:'serialNo'},
{name:'describe1'}, 
{name:'describe2'},  
{name:'describe3'},
{name:'itemDescribe'},
{name:'itemValue'},
{name:'itemWeight'}
]) 
}); 
ds.load(); 


var grid=new Ext.grid.EditorGridPanel({ 
id:'myCustomers',
stripeRows:true,//斑马线效果
title:'<div align="center">已添加的特征项</div>', 
height:500, 
cm:cm,
store: ds, 
clicksToEdit: 2,
columnLines:true, 
cls: 'grid-row-span',
loadMask: { msg: "加载中..." }, 
viewConfig: {   
                            forceFit:true   
            }  
                      
});

function afterEdit(obj){    //每次更改后，触发一次该事件  
	var row = obj.record;
	var col = obj.field;

 //  alert("serialNo" +row.data.serialNo +",orginalValue:"+obj.originalValue+",value:"+obj.value);  
   
	Ext.Ajax.request({
		url:'model/editAdjustment.do?dataFlag=3',
		params : { serialNo: row.data.serialNo,
			field:col, 
			describe2:row.data.describe2,
			originalValue:obj.originalValue, 
			modifiedValue:obj.value
		},
		method:'POST',
		success:function(){
			Ext.MessageBox.alert("提示","修改成功！");
			grid.store.reload(); 
		},failure:function(){
			Ext.MessageBox.alert("提示", "修改失败！"); 
		}
	});
 
}  
grid.on("afteredit", afterEdit, grid);  

var panel =  new Ext.Panel({
renderTo:"panel",
items:[addForm, grid]
});


}); 